package com.ztesoft.sca.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.ztesoft.sca.dao.AiChatAudioDao;
import com.ztesoft.sca.model.aichat.*;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import com.ztesoft.sca.common.Constants;
import com.ztesoft.sca.util.exception.ExceptionUtil;
import com.ztesoft.sca.util.message.ResponseUtils;
import com.ztesoft.sca.util.seq.SequenceCreator;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/*
 * zxz 音频源配置
 * */

@RestController
public class AiChatAudioController {
	private static final Logger logger = Logger.getLogger(AiChatAudioController.class);
    @Autowired
    private AiChatAudioDao aiChatAudioDao;    
	
    /*
     * zxz 音频源列表 
     * */
	@RequestMapping(value = "/aichataudio/list",method = {RequestMethod.GET})
	public String AiChatAudioList(@RequestParam int page,@RequestParam int rownum,HttpServletRequest request) {
		logger.info("-------aichataudio--list-----");
		long id=0l;
		int from=page;//第1页开始
		int size=rownum; //每页大小  
		String type="";
        SimpleDateFormat time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		List<AiChatDto> list = new ArrayList<>();
		try {
			list = this.aiChatAudioDao.listAiChatAudio(id,type,from,size);
		} catch (Exception e) {
			 logger.error(" aiChatAudioList occur an error!",e);
			 return ResponseUtils.feedback(Constants.INF_CODE_ERROR,Constants.INF_DESC_ERROR, ExceptionUtil.getMessage(e));
		}
        JSONArray json = new JSONArray();
        for(AiChatDto pLog : list){
            JSONObject jo = new JSONObject();
            jo.put("id", pLog.getId());
            jo.put("name", pLog.getName());
            jo.put("type", pLog.getType());
            jo.put("request_action", pLog.getRequestAction());
            jo.put("description", pLog.getDescription());
            jo.put("create_time", time.format(pLog.getCreateTime()));
            json.add(jo);
        }
		JSONObject jsonStrAll = new JSONObject();
		int records=list.size();
		int total=records/size;
		//int page=records/size;
		jsonStrAll.put("rows", json);
		jsonStrAll.put("page", page);
		//jsonStrAll.put("total", total);
		jsonStrAll.put("records", records);
        return ResponseUtils.feedback(Constants.INF_CODE_SUCC,Constants.INF_DESC_SUCC,jsonStrAll);	
	}
	
	/*
	 * zxz 音频源 新增
	 * */
	@RequestMapping(value = "/aichataudio/add",method = {RequestMethod.POST})
	public String AiChatAudioAdd(@RequestBody String requestBody,HttpServletRequest request) {
		logger.info("-------aichataudio--add-----");
        JSONObject json = JSONObject.fromObject(requestBody);
        long Id = 0l;
        try {
            Id = SequenceCreator.getSequence("SCA_AUDIO_SOURCE_SEQ");
       } catch (Exception e) {
           logger.error("sequenceCreator obtain SCA_AUDIO_SOURCE occur an error!",e);
           return ResponseUtils.feedback(Constants.INF_CODE_ERROR,Constants.INF_DESC_ERROR,ExceptionUtil.getMessage(e));
       }
       String name = json.getString("name");
       String type = json.getString("type");
       String request_action = json.getString("request_action");
       String file_path = json.getString("file_path");
       String server_name = json.getString("server_name");
       String server_password = json.getString("server_password");
       String format = json.getString("format");
       String server_ip = json.getString("server_ip");
       String sample_rate = json.getString("sample_rate");
       String description = json.getString("description");
       
       AiChatDto aiChat = new AiChatDto();
       aiChat.setId(Id);
       aiChat.setName(name);
       aiChat.setType(type);
       aiChat.setRequestAction(request_action);
       aiChat.setFilePath(file_path);
       aiChat.setServerName(server_name);
       aiChat.setServerPassword(server_password);
       aiChat.setFormat(format);
       aiChat.setServerIp(server_ip);
       aiChat.setSampleRate(sample_rate);
       aiChat.setDescription(description);
       aiChat.setCreateTime(new Date());
       try {
           this.aiChatAudioDao.insertAiChatAudio(aiChat);
       } catch (Exception e) {
           logger.error("create aiChatAudio occur an error !",e);
           return ResponseUtils.feedback(Constants.INF_CODE_ERROR,Constants.INF_DESC_ERROR,ExceptionUtil.getMessage(e));
       }
       return ResponseUtils.feedback(Constants.INF_CODE_SUCC,Constants.INF_DESC_SUCC,aiChat);
	}
	
    /*
     * zxz 音频转换组件列表
     * */
	@RequestMapping(value = "/aichataudio/listconvert",method = {RequestMethod.GET})
	public String AiChatAudioConvertList(@RequestParam int page,@RequestParam int rownum,HttpServletRequest request) {
		logger.info("-------aichataudioConvert--list-----");
		logger.info("--------"+page+"--rownum--"+rownum);
		long convert_id=0l;
		int from=page;//第1页开始
		int size=rownum; //每页大小
		String type="";
        SimpleDateFormat time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		List<AiChatDto> list = new ArrayList<>();
		try {
			list = this.aiChatAudioDao.listAiChatAudioConvert(convert_id,type,from,size);
		} catch (Exception e) {
			 logger.error(" aiChatAudioList occur an error!",e);
			 return ResponseUtils.feedback(Constants.INF_CODE_ERROR,Constants.INF_DESC_ERROR, ExceptionUtil.getMessage(e));
		}
        JSONArray json = new JSONArray();
        for(AiChatDto pLog : list){
            JSONObject jo = new JSONObject();
            jo.put("convert_id", pLog.getConvertId());
            jo.put("convert_name", pLog.getConvertName());
            jo.put("convert_class", pLog.getConvertClass());
            jo.put("convert_method", pLog.getConvertMethod());
            jo.put("convert_extend_para", pLog.getConvertExtendPara());
            jo.put("convert_description", pLog.getConvertDescription());
            json.add(jo);
        }
		JSONObject jsonStrAll = new JSONObject();
		int records=list.size();
		int total=records/size;
		//int page=records/size;
		jsonStrAll.put("rows", json);
		jsonStrAll.put("page", page);
		//jsonStrAll.put("total", total);
		jsonStrAll.put("records", records);
        return ResponseUtils.feedback(Constants.INF_CODE_SUCC,Constants.INF_DESC_SUCC,jsonStrAll);	
	}

	/*
	 * zxz 音频转换组件 新增
	 * */
	@RequestMapping(value = "/aichataudio/addconvert",method = {RequestMethod.POST})
	public String AiChatAudioConvertAdd(@RequestBody String requestBody,HttpServletRequest request) {
		logger.info("-------aichataudio--addconvert-----");
        JSONObject json = JSONObject.fromObject(requestBody);
        long convert_id = 0l;
        try {
        	convert_id = SequenceCreator.getSequence("SCA_AUDIO_CONVERT_SEQ");
       } catch (Exception e) {
           logger.error("sequenceCreator obtain SCA_AUDIO_CONVERT_SEQ occur an error!",e);
           return ResponseUtils.feedback(Constants.INF_CODE_ERROR,Constants.INF_DESC_ERROR,ExceptionUtil.getMessage(e));
       }
       String convert_name = json.getString("convert_name");
       String convert_class = json.getString("convert_class");
       String convert_method = json.getString("convert_method");
       String convert_extend_para = json.getString("convert_extend_para");
       String convert_description = json.getString("convert_description");
       
       AiChatDto aiChat = new AiChatDto();
       aiChat.setConvertId(convert_id);
       aiChat.setConvertName(convert_name);
       aiChat.setConvertClass(convert_class);
       aiChat.setConvertMethod(convert_method);
       aiChat.setConvertExtendPara(convert_extend_para);
       aiChat.setConvertDescription(convert_description);
       aiChat.setconvertCreateTime(new Date());
       try {
           this.aiChatAudioDao.insertAiChatAudioConvert(aiChat);
       } catch (Exception e) {
           logger.error("create aiChatAudioConvert occur an error !",e);
           return ResponseUtils.feedback(Constants.INF_CODE_ERROR,Constants.INF_DESC_ERROR,ExceptionUtil.getMessage(e));
       }
       return ResponseUtils.feedback(Constants.INF_CODE_SUCC,Constants.INF_DESC_SUCC,aiChat);
	}
	
}
